home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------------------------
- // Camera Functions
- //
- // by Philip McBride
- //
- //--------------------------------------------------------------------------------------------
-
-
- #include <FixMath.h>
- #include "GameControls.h"
- #include "extern.h"
- #include "camera.h"
-
- //--------------------------------------------------------------------------------------------
- // Create a Camera (with default settings) -- used if no camera included in file
- //
- TQ3CameraObject MyNewCamera(CWindowPtr theWindow)
- {
- TQ3ViewAngleAspectCameraData perspectiveData;
- TQ3CameraObject camera;
- float fieldOfView = .50;
- TQ3Status returnVal = kQ3Failure ;
-
- // Assign default placement and range
- perspectiveData.cameraData.placement.cameraLocation = kMyDefaultFrom;
- perspectiveData.cameraData.placement.pointOfInterest = kMyDefaultTo;
- perspectiveData.cameraData.placement.upVector = kMyDefaultUp;
- perspectiveData.cameraData.range.hither = kMyDefaultHither;
- perspectiveData.cameraData.range.yon = kMyDefaultYon;
-
- // Assign standard viewport
- perspectiveData.cameraData.viewPort.origin.x = -1.0;
- perspectiveData.cameraData.viewPort.origin.y = 1.0;
- perspectiveData.cameraData.viewPort.width = 2.0;
- perspectiveData.cameraData.viewPort.height = 2.0;
-
- perspectiveData.fov = fieldOfView;
- perspectiveData.aspectRatioXToY =
- (float) (theWindow->portRect.right - theWindow->portRect.left) /
- (float) (theWindow->portRect.bottom - theWindow->portRect.top);
-
- camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
-
- return camera ;
- }
-
- //--------------------------------------------------------------------------------------------
- // Get a Model's Bounding Box
- //
- void MyGetBoundingBox(DocumentPtr theDocument, TQ3GroupObject mainGroup, TQ3BoundingBox *viewBBox)
- {
- TQ3Status status;
- TQ3ViewObject viewObject = theDocument->theView;
-
- Q3View_StartBoundingBox(viewObject,kQ3ComputeBoundsApproximate);
- do {
- status = Q3DisplayGroup_Submit(mainGroup, viewObject);
- } while (Q3View_EndBoundingBox(viewObject, viewBBox) == kQ3ViewStatusRetraverse);
- }
-
- //--------------------------------------------------------------------------------------------
- // Get the Camera Data (initialize for new file and camera)
- //
- // Get the camera information from the current view and initialize the
- // camera related fields of the document record
- void MyGetCameraData(DocumentPtr theDocument, TQ3CameraObject theCamera)
- {
- TQ3CameraPlacement cameraPlacement;
-
- // Get the camera data.
- Q3Camera_GetPlacement(theCamera, &cameraPlacement);
-
- // Set the document's camera data.
- theDocument->cameraLocation = cameraPlacement. cameraLocation;
- theDocument->pointOfInterest = cameraPlacement. pointOfInterest;
- theDocument->yVector = cameraPlacement. upVector;
-
- // Calculate the x and z vectors and assign them to the document.
- Q3Point3D_Subtract(&theDocument->pointOfInterest,
- &theDocument->cameraLocation, &theDocument->zVector);
- Q3Vector3D_Cross(&theDocument->zVector, &theDocument->yVector,
- &theDocument->xVector);
-
- Q3Vector3D_Normalize(&theDocument->xVector, &theDocument->xVector);
- }
-
- //--------------------------------------------------------------------------------------------
- // Set the Camera'a Data (when moved)
- //
- // Set the camera of the current view to the values in the document record
- void MySetCameraData(DocumentPtr theDocument, TQ3CameraObject theCamera)
- {
- TQ3CameraPlacement cameraPlacement;
-
- // Set the camera placement data.
- cameraPlacement.cameraLocation = theDocument->cameraLocation;
- cameraPlacement.pointOfInterest = theDocument->pointOfInterest;
- cameraPlacement.upVector = theDocument->yVector;
-
- // Set the camera data to the camera.
- Q3Camera_SetPlacement(theCamera, &cameraPlacement);
- }
-
- //--------------------------------------------------------------------------------------------
- // Move Camera X
- //
- // Move the camera along the X axis
- void MyMoveCameraX(DocumentPtr theDocument, float dX)
- {
- TQ3ViewObject theView;
- TQ3CameraObject theCamera;
- TQ3Vector3D scaledVector;
- TQ3Point3D newPoint;
-
- // Get the view and the camera objects.
- theView = theDocument->theView;
- Q3View_GetCamera(theView, &theCamera);
-
- // Scale the X vector to make it dX longer.
- Q3Vector3D_Scale(&theDocument->xVector,dX/Q3Vector3D_Length(&theDocument->xVector),
- &scaledVector);
-
- // Move the camera position and direction by the new vector.
- Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
- theDocument->cameraLocation = newPoint;
- Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
- theDocument->pointOfInterest = newPoint;
-
- // Set the updated camera data to the camera.
- MySetCameraData(theDocument, theCamera);
-
- // Update the view with the changed camera and dispose of the camera.
- Q3View_SetCamera(theView, theCamera);
- Q3Object_Dispose(theCamera);
- }
-
- //--------------------------------------------------------------------------------------------
- // Move Camera Y
- //
- // Move the camera along the Y axis
- void MyMoveCameraY(DocumentPtr theDocument, float dY)
- {
- TQ3ViewObject theView;
- TQ3CameraObject theCamera;
- TQ3Vector3D scaledVector;
- TQ3Point3D newPoint;
-
- // Get the view and the camera objects.
- theView = theDocument->theView;
- Q3View_GetCamera(theView, &theCamera);
-
- // Scale the Y vector to make it dY longer.
- Q3Vector3D_Scale(&theDocument->yVector,dY/Q3Vector3D_Length(&theDocument->yVector),
- &scaledVector);
-
- // Move the camera position and direction by the new vector.
- Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
- theDocument->cameraLocation = newPoint;
- Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
- theDocument->pointOfInterest = newPoint;
-
- // Set the updated camera data to the camera.
- MySetCameraData(theDocument, theCamera);
-
- // Update the view with the changed camera and dispose of the camera.
- Q3View_SetCamera(theView, theCamera);
- Q3Object_Dispose(theCamera);
- }
-
- //--------------------------------------------------------------------------------------------
- // Move Camera Z
- //
- // Move the camera along the Z axis
- void MyMoveCameraZ(DocumentPtr theDocument, float dZ)
- {
- TQ3ViewObject theView;
- TQ3CameraObject theCamera;
- TQ3Vector3D scaledVector;
- TQ3Point3D newPoint;
-
- // Get the view and the camera objects.
- theView = theDocument->theView;
- Q3View_GetCamera(theView, &theCamera);
-
- // Scale the Z vector to make it dZ longer.
- Q3Vector3D_Scale(&theDocument->zVector,dZ/Q3Vector3D_Length(&theDocument->zVector),
- &scaledVector);
-
- // Move the camera position and direction by the new vector.
- Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
- theDocument->cameraLocation = newPoint;
- Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
- theDocument->pointOfInterest = newPoint;
-
- // Set the updated camera data to the camera.
- MySetCameraData(theDocument, theCamera);
-
- // Update the view with the changed camera and dispose of the camera.
- Q3View_SetCamera(theView, theCamera);
- Q3Object_Dispose(theCamera);
- }
-
- //--------------------------------------------------------------------------------------------
- // Rotate Camera X
- //
- // Rotate the camera about the X axis
- void MyRotateCameraX(DocumentPtr theDocument, float dX)
- {
- TQ3ViewObject theView;
- TQ3CameraObject theCamera;
- TQ3Vector3D rotatedVector;
- TQ3Matrix4x4 rotationMatrix;
-
- // Get the view and the camera objects.
- theView = theDocument->theView;
- Q3View_GetCamera(theView, &theCamera);
-
- // Create the rotation matrix for rotating about the x axis.
- Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
- &theDocument->cameraLocation, &theDocument->xVector, dX);
-
- // Rotate the y vector (up vector) about the x axis.
- Q3Vector3D_Transform(&theDocument->yVector, &rotationMatrix,
- &rotatedVector);
- theDocument->yVector = rotatedVector;
-
- // Rotate the z vector about the x axis.
- Q3Vector3D_Transform(&theDocument->zVector, &rotationMatrix,
- &rotatedVector);
- theDocument->zVector = rotatedVector;
-
- // Update the point of interest from the new z vector.
- Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,
- &theDocument->zVector, &theDocument->pointOfInterest);
-
- // Set the updated camera data to the camera.
- MySetCameraData(theDocument, theCamera);
-
- // Update the view with the changed camera and dispose of the camera.
- Q3View_SetCamera(theView, theCamera);
- Q3Object_Dispose(theCamera);
- }
-
- //--------------------------------------------------------------------------------------------
- // Rotate Camera Y
- //
- // Rotate the camera about the Y axis
- void MyRotateCameraY(DocumentPtr theDocument, float dY)
- {
- TQ3ViewObject theView;
- TQ3CameraObject theCamera;
- TQ3Vector3D rotatedVector;
- TQ3Matrix4x4 rotationMatrix;
-
- // Get the view and the camera objects.
- theView = theDocument->theView;
- Q3View_GetCamera(theView, &theCamera);
-
- // Create the rotation matrix for rotating about the y axis.
- Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
- &theDocument->cameraLocation, &theDocument->yVector, dY);
-
- // Rotate the z vector about the y axis.
- Q3Vector3D_Transform(&theDocument->zVector, &rotationMatrix,
- &rotatedVector);
- theDocument->zVector = rotatedVector;
-
- // Rotate the x vector about the y axis.
- Q3Vector3D_Transform(&theDocument->xVector, &rotationMatrix,
- &rotatedVector);
- theDocument->xVector = rotatedVector;
-
- // Update the point of interest from the new z vector.
- Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,
- &theDocument->zVector, &theDocument->pointOfInterest);
-
- // Set the updated camera data to the camera.
- MySetCameraData(theDocument, theCamera);
-
- // Update the view with the changed camera and dispose of the camera.
- Q3View_SetCamera(theView, theCamera);
- Q3Object_Dispose(theCamera);
- }
-
- //--------------------------------------------------------------------------------------------
- // Rotate Camera Z
- //
- // Rotate the camera about the Z axis
- void MyRotateCameraZ(DocumentPtr theDocument, float dZ)
- {
- TQ3ViewObject theView;
- TQ3CameraObject theCamera;
- TQ3Vector3D rotatedVector;
- TQ3Matrix4x4 rotationMatrix;
-
- // Get the view and the camera objects.
- theView = theDocument->theView;
- Q3View_GetCamera(theView, &theCamera);
-
- // Create the rotation matrix for rotating about the z axis.
- Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
- &theDocument->cameraLocation, &theDocument->zVector, dZ);
-
- // Rotate the y vector (up vector) about the z axis.
- Q3Vector3D_Transform(&theDocument->yVector, &rotationMatrix,
- &rotatedVector);
- theDocument->yVector = rotatedVector;
-
- // Rotate the x vector about the z axis.
- Q3Vector3D_Transform(&theDocument->xVector, &rotationMatrix,
- &rotatedVector);
- theDocument->xVector = rotatedVector;
-
- // Set the updated camera data to the camera.
- MySetCameraData(theDocument, theCamera);
-
- // Update the view with the changed camera and dispose of the camera.
- Q3View_SetCamera(theView, theCamera);
- Q3Object_Dispose(theCamera);
- }
-
-